﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyVoteMVC.BackstageManagement.Views;
using MyVoteMVC.Service;
using MyVoteMVC.Common;

namespace MyVoteMVC.BackstageManagement.Controllers
{
    public class DictionaryManagementController : IDictionaryManagementController
    {
        IDictionaryManagementView view;
        IDictionariesService dictionariesService;
        public DictionaryManagementController(IDictionaryManagementView view)
        {
            this.view = view;
            dictionariesService = Factory.GetInstance<IDictionariesService>();
        }

        #region IDictionaryManagementController Members

        public void AddDictionary(string dName, string dtype, string dValue, int dOrder)
        {
            if (string.IsNullOrWhiteSpace(dName))
            {
                view.OnSubmitError("名字不为空");
                return;
            }
            if (string.IsNullOrWhiteSpace(dtype))
            {
                view.OnSubmitError("类型不为空");
                return;
            }
            if (string.IsNullOrWhiteSpace(dValue))
            {
                view.OnSubmitError("值不为空");
                return;
            }
            if (dOrder <1)
            {
                view.OnSubmitError("排序不小于1");
                return;
            }

            if (Encoding.Default.GetByteCount(dName) > 50)
            {
                view.OnSubmitError("名字过长");
                return;
            }
            if (Encoding.Default.GetByteCount(dtype) > 50)
            {
                view.OnSubmitError("类型过长");
                return;
            }
            if (Encoding.Default.GetByteCount(dValue) > 50)
            {
                view.OnSubmitError("值过长");
                return;
            }

            try
            {
                dictionariesService.AddDictionary(dName, dtype, dValue, dOrder);
                view.AddDictionaryComplete("新增成功");
            }
            catch (ArgumentException ex)
            {
                view.OnSubmitError(ex.Message);
            }
            catch (Exception ex)
            {
                view.OnSubmitError("新增出错");
            }
        }

        public void ModifyDictionary(string dID, string dName, string dtype, string dValue, int dOrder)
        {
            if (string.IsNullOrWhiteSpace(dName))
            {
                view.OnSubmitError("名字不为空");
                return;
            }
            if (string.IsNullOrWhiteSpace(dtype))
            {
                view.OnSubmitError("类型不为空");
                return;
            }
            if (string.IsNullOrWhiteSpace(dValue))
            {
                view.OnSubmitError("值不为空");
                return;
            }
            if (dOrder < 1)
            {
                view.OnSubmitError("排序不小于1");
                return;
            }

            if (Encoding.Default.GetByteCount(dName) > 50)
            {
                view.OnSubmitError("名字过长");
                return;
            }
            if (Encoding.Default.GetByteCount(dtype) > 50)
            {
                view.OnSubmitError("类型过长");
                return;
            }
            if (Encoding.Default.GetByteCount(dValue) > 50)
            {
                view.OnSubmitError("值过长");
                return;
            }

            try
            {
                if (dictionariesService.ModifyDictionary(dID, dName, dtype, dValue, dOrder))
                { view.ModifyDictionaryComplete("修改成功"); }
                else
                {
                    view.ModifyDictionaryComplete("不存在这条记录");
                }
            }
            catch (ArgumentException ex)
            {
                view.OnSubmitError(ex.Message);
            }
            catch (Exception ex)
            {
                view.OnSubmitError("修改出错");
            }
        }

        public void DeleteDictionary(string dID)
        {
            try
            {
                dictionariesService.DeleteDictionary(dID);
                view.DeleteDictionaryComplete("删除成功");
            }
            catch (Exception ex)
            {
                view.OnSubmitError("删除出错");
            }
        }

        public void GetDictionaries()
        {
            view.GetDictionariesComplete(dictionariesService.GetAll());
        }

        public void GetByID(string dID)
        {
            view.GetByIDComplete(dictionariesService.GetByID(dID));
        }

        #endregion
    }
}
